# Title     : TODO
# Objective : TODO
# Created by: Administrator
# Created on: 2019/6/18

library(optparse)
library(tidyverse)
library(magrittr)

option_list <- list(
  make_option("--i", default = "AllMet.csv", type = "character", help = "metabolite data file"),
  make_option("--g", default = "SampleInfo.csv", type = "character", help = "sample group file"),
  make_option("--sc", default = "sample_color.txt", type = "character", help = "sample color file")
)
opt <- parse_args(OptionParser(option_list = option_list))

sampleInfo <- read.csv(opt$g, header = T, stringsAsFactors = F) %>%
  select(c("SampleID", "ClassNote")) %>%
  as.data.frame()

sampleColDf <- read_csv(opt$sc) %>%
  select(c("ClassNote", "col")) %>%
  filter(ClassNote == "QC") %>%
  add_row(ClassNote = "Sample", col = "black")
sampleCols <- sampleColDf %>%
  deframe()
sampleCols

plotData <- read_csv("PCA_with_All_Samples_Score.csv") %>%
  left_join(sampleInfo, by = "SampleID") %>%
  mutate(id = 1:n()) %>%
  mutate_at(vars("ClassNote"), ~ifelse(.x != "QC", "Sample", "QC"))

sd <- sd(plotData$PC1)
neg3Sd <- (-3 * sd)
neg2Sd <- (-2 * sd)
pos3Sd <- (3 * sd)
pos2Sd <- (2 * sd)
sd2Color <- "#FFA500"
sd3Color <- "red"
sdFontSize <- 1.9
xend <- nrow(plotData)
pdf("PC1_Dotplot.pdf", width = 8, height = 3)
textPos <- 0.8

print("=plotdata=")
print(plotData)

p <- ggplot(plotData, mapping = aes(x = id, y = PC1)) +
  xlab("") +
  ylab("PC1 scores/Outlier Detection") +
  theme_bw(base_size = 8.8, base_family = "Times") +
  theme(axis.text.x = element_text(angle = 90, size = 6, hjust = 1, vjust = 0.5),
        axis.text.y = element_text(size = 8.8), legend.position = 'top', panel.grid.minor = element_blank(),
        axis.title.y = element_text(size = 11), legend.margin = margin(t = 0.3, b = -0.3, unit = 'cm'),
        legend.text = element_text(size = 8)
  ) +
  #-3sd
  geom_segment(aes(x = 1, xend = xend, y = neg3Sd, yend = neg3Sd), colour = "red", linetype = "dashed") +
  annotate("text", x = textPos, y = neg3Sd, label = "-3SD", color = sd3Color, hjust = 1, size = sdFontSize) +
  #-2sd
  geom_segment(aes(x = 1, xend = xend, y = neg2Sd, yend = neg2Sd), colour = "#FFA500", linetype = "dashed") +
  annotate("text", x = textPos, y = neg2Sd, label = "-2SD", color = sd2Color, hjust = 1, size = sdFontSize) +
  #2sd
  geom_segment(aes(x = 1, xend = xend, y = pos2Sd, yend = pos2Sd), colour = "#FFA500", linetype = "dashed") +
  annotate("text", x = textPos, y = pos2Sd, label = "+2SD", color = sd2Color, hjust = 1, size = sdFontSize) +
  #3sd
  geom_segment(aes(x = 1, xend = xend, y = pos3Sd, yend = pos3Sd), colour = "red", linetype = "dashed") +
  annotate("text", x = textPos, y = pos3Sd, label = "+3SD", color = sd3Color, hjust = 1, size = sdFontSize) +
  #0 line
  geom_segment(aes(x = 1, xend = xend, y = 0, yend = 0), colour = "#BEBEBE", linetype = "solid") +
  #point
  geom_point(aes(colour = factor(ClassNote))) +
  #lengend
  scale_colour_manual("", values = sampleCols) +
  scale_x_continuous("", labels = as.character(plotData$SampleID), breaks = plotData$id)
p
dev.off()




